<?php

/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2016/9/28
 * Time: 14:23
 */
class ObjectThis
{
    public $newObj;
    function __construct($array = [])
    {
        $this->newObj = $this->objectThis($array);
    }

    public function objectThis($array = null)
    {
        if (!$array) {
            foreach ($this as $property_name => $property_values) {
                if (is_array($property_values) && !empty($property_values)) {
                    $this->{$property_name} = $this->objectThis($property_values);
                } else if (is_array($property_values) && empty($property_values)) {
                    $this->{$property_name} = new stdClass();
                }
            }
        } else {
            $object = new stdClass();
            foreach ($array as $index => $values) {
                if (is_array($values) && empty($values)) {
                    $object->{$index} = new stdClass();
                } else if (is_array($values)) {
                    $object->{$index} = $this->objectThis($values);
                } else {
                    $object->{$index} = $values;
                }
            }
            return $object;
        }
    }
}

$arr = [
    'id' => 1,
    'arr' => [
        'test_key' => 'test_value'
    ]
];
$ot = new ObjectThis($arr);
var_dump($ot->newObj);
//$o = $ot->objectThis($arr);
//var_dump($o);
//echo is_object($o);